import hasOwnProperty from 'shared/hasOwnProperty';
import { REACT_ELEMENT_TYPE } from 'shared/ReactSymbols';
const RESERVED_PROPS = {
    key: true,
    ref: true,
    __self: true,
    __source: true,
};

function hasValidKey(config) {
    return config.key !== undefined;
}

function hasValidRef(config) {
    return config.ref !== undefined;
}

function ReactElement(type, key, ref, props) {
    // react元素,也就是虚拟DOM
    return {
        $$typeof: REACT_ELEMENT_TYPE,
        key,
        ref,
        props,
        type,
    };
};

export function jsxDEV(type, config, maybeKey) {
    let propName; // 属性名
    const props = {}; // 属性对象
    let key = null; // 每个虚拟DOM可以有一个可选的key属性，用来区分一个父节点下的不同节点
    let ref = null; // 引用，可以通过ref获取真实DOM
    if (typeof maybeKey !== 'undefined') {
        key = maybeKey;
    }
    if (hasValidRef(config)) {
        ref = config.ref;
    }

    for (propName in config) {
        if (hasOwnProperty.call(config, propName)
            && !RESERVED_PROPS.hasOwnProperty(propName)) {
            props[propName] = config[propName];
        }
    }
    return ReactElement(type, key, ref, props);
};